package com.baiytfp.hf.HFDataExchange.action.controller.view;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baiytfp.hf.baiytfp.common.util.DateUtil;
import com.baiytfp.hf.baiytfp.common.util.StringUtil;
import com.baiytfp.hf.baiytfp.framework.util.PropertiesUtil;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;
import com.baiytfp.hf.baiytfp.hfutils.HFDate;
import com.baiytfp.hf.baiytfp.hfutils.HFRequest;
import com.baiytfp.hf.HFDataExchange.common.base.anotation.Auth;
import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.HFDataExchange.common.model.table.User_account;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.baiytfp.hf.baiytfp.framework.shiro.ShiroUtil;
import com.baiytfp.hf.baiytfp.hfutils.HFConfig;
import com.baiytfp.hf.baiytfp.hfutils.HFWeb;
import com.baiytfp.hf.HFDataExchange.common.utils.PathInfo;

import static com.baiytfp.hf.HFDataExchange.common.utils.WebRandomCodeUtils.isClose;

@Controller
@RequestMapping("/view/index")
public class IndexVController {
	@RequestMapping("/welcome")
	public ModelAndView welcome(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView();

		mav.addObject("user", ShiroUtil.getUserCode());

		// User_account users = new User_account();
		// users.setUser_code("admin Steven");
		//
		// System.out.println("BBB : " +
		// (byte[])SerializeUtil.serialize(users));
		// System.out.println("XXX : " +
		// ((User_account)SerializeUtil.deserialize((byte[])SerializeUtil.serialize(users))).getUser_code());
		//
		// CacheUtil.getCache().set("TEST1", "XXX");
		// CacheUtil.getCache().set("TEST2", SerializeUtil.serialize(users));
		//
		// org.apache.commons.codec.binary.Base64 base64 = new Base64();
		// User_account s =
		// (User_account)(SerializeUtil.deserialize((base64.decodeBase64((String)CacheUtil.getCache().get("TEST2")))));
		// mav.addObject("user", s.getUser_code() );

		return mav;
	}


	// 空白页面
	@RequestMapping("/blank")
	public ModelAndView blank(HttpServletRequest request,
							  HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView();

		return mav;
	}

	// 登录
	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// HFWeb.getIstance().logout(request);

		String user_code = HFWeb.getIstance().getUserCode();
		
		if (StringUtil.isNull(user_code)) {
			ModelAndView mav = new ModelAndView();
			mav.addObject("status", HFRequest.getIstance().getInt(request, "status", 0) );
			mav.addObject("isClose", isClose());
			// 设置登录的背景页面
			mav.addObject(
					"loginbgUrl",
					HFConfig.getIstance().getPropConfig(
							"loginbg_url",
							PathInfo.getWebRoot(request)
									+ "view/index/loginbg.do"));

			return mav;
		} else {
			// 已经登录直接进入首页
			response.sendRedirect(request.getContextPath()
					+ "/view/admin/index.do");
		}

		return null;
	}

	// 登录背景页面
	@RequestMapping("/loginbg")
	public ModelAndView loginbg(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView();

		return mav;
	}

	// 登录输入页面
	@RequestMapping("/logininput")
	public ModelAndView logininput(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView();
		mav.addObject("status", HFRequest.getIstance().getInt(request, "status", 0) );
		mav.addObject("login_logo", HFConfig.getIstance().getDBConfig("LOGIN_LOGO", PathInfo.getSkinPath(request) + "hongfeng/logo.png"));

		return mav;
	}

	// 未授权时跳转的页面
	@RequestMapping("/unauthorized")
	public ModelAndView unauthorized(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView();

		return null;
	}

}
